import 'package:eshi/app/CommonFunctions.dart';
import 'package:eshi/app/shi/data/HistoryMarks.dart';
import 'package:eshi/app/shi/taxis/TabEditPage.dart';
import 'package:flutter/material.dart';

//mark手动编辑
class MarkEdit extends StatefulWidget{
  TabEditPageState tabEdit;
  MarkEdit(this.tabEdit, {super.key});

  @override
  State<StatefulWidget> createState() => MarkEditState();
  
}

class MarkEditState extends State<MarkEdit>{
   var types = HistoryMarks.values;
  Map<String,TextEditingController> tcMap={};
  Map<String,FocusNode> fnMap={};
  get marks => widget.tabEdit.widget.marks;
  get absValue{
      List<String> rt =[];
      tcMap.forEach((k,v){
        if(v.text.isNotEmpty) {
          rt.add("$k:${v.text}");
        }
      });
      return rt;
  } 
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for (var p in types) {
      var name = p.name;
      var value = marks?[name]?.join(",");
      var tc = tcMap[name] = TextEditingController()
        ..text = value ?? "";
      fnMap[name] = FocusNode()
        ..addListener((){
          var focusNode =fnMap[name]!;
          if(focusNode.hasFocus){
               tc.selection = TextSelection.collapsed(
                offset: tc.text.length
              );
          }else{
              if(tc.text!=value) {
                marks[name] = tc.text.split(",");
                widget.tabEdit.marksGroup["absValue"] = absValue;
              }
          }
          setState(() {
                  
          });
          print("focus changed:$name ${focusNode.hasFocus}");
        });
    }
  }
  @override
  void dispose(){
    super.dispose();
    tcMap.forEach((name,tc) => tc.dispose() );
    fnMap.forEach((name,tc) => tc.dispose() );
  }

  upMarks(){
    for (var p in types) {
      var name = p.name;
      var value = marks?[name]?.join(",");
      tcMap[name]!.text = value ?? "";
    }
  }

  Widget InputText(String name){
      var tc =  tcMap[name]!;
      var focusNode = fnMap[name]!;
      return TextField(
        controller: tc, 
        focusNode: focusNode,
        keyboardType: TextInputType.multiline,
        maxLines: focusNode.hasFocus? 20 : 1,
        minLines: 1,
        showCursor:true,
        decoration:  InputDecoration(
                        // border: OutlineInputBorder(), 
        )
      );
  }
  
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
      var types = HistoryMarks.values;
      return Container(
        padding: EdgeInsets.only(left: 10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
                Text(mt("文件引用"),style: TextStyle(fontSize: 20),),
                Row(
                  spacing: 4,
                  children: [
                    IconButton(onPressed: () async {
                        await widget.tabEdit.onLoadFile(false); 
                        upMarks();
                    }, icon:  Icon(Icons.file_upload)),
                    IconButton(onPressed: () async {
                        widget.tabEdit.marksAdds(widget.tabEdit.widget.data);
                        await Future.delayed(Duration(seconds: 2));
                        upMarks();
                    }, icon:  Icon(Icons.send )),
                    IconButton(onPressed: (){
                        Navigator.pop(context);
                    }, icon:  Icon(Icons.close))
                ],)
            ]
          ),
          const Divider(height: 1),
          for(var type in types)
            Row(children: [
              Text(type.title),
              SizedBox(width: 10),
              Expanded(child: InputText(type.name)),
              SizedBox(width: 5),
            ],)

        ]));
  }

}